Skip to content

fix(graphql): Optimize N+1 queries for events#506

Draft
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/graphql-n-plus-1-events
Draft

fix(graphql): Optimize N+1 queries for events#506
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/graphql-n-plus-1-events

Conversation

@sentry

@sentry sentry Bot commented May 20, 2026

Copy link
Copy Markdown
Contributor

This PR addresses an N+1 query performance issue when fetching event lists via the GraphQL API, specifically impacting pEvent related fields such as organisation, nextOccurrenceDatetime, lastOccurrenceDatetime, and hasSpaceForEnrolments.

Problem:
Previously, when querying a list of events, each event's pEvent resolver would trigger individual database queries for its associated PalvelutarjotinEvent and its related organisation and occurrence data. This resulted in a high number of database queries (N+1 pattern), significantly degrading performance for event list requests.

Solution:

  1. Bulk Fetch organisation: Added .select_related("organisation") to the PalvelutarjotinEvent queryset in graphene_linked_events/schema.py to fetch related organisation data in a single JOIN.
  2. Optimize Occurrence Resolvers: Modified resolve_next_occurrence_datetime and resolve_last_occurrence_datetime in occurrences/schema.py to perform filtering on already prefetched occurrences in Python, rather than issuing new database queries for each event.
  3. Optimize has_space_for_enrolments: Updated the PalvelutarjotinEvent.has_space_for_enrolments() method in occurrences/models.py to also utilize Python-side filtering on prefetched occurrences.

These changes ensure that related data is fetched efficiently, drastically reducing the number of database queries for event list operations.

Fixes KULTUS-API-5

@sonarqubecloud

Copy link
Copy Markdown

@azure-pipelines

Copy link
Copy Markdown

KULTUS-API branch is deployed to platta: https://kultus-pr506.api.dev.hel.ninja 🚀🚀🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants